package com.transaction.manage.controller;

import com.transaction.common.base.BaseController;
import com.transaction.common.base.User;
import com.transaction.common.util.JwtTokenUtil;
import com.transaction.manage.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 * @author: dy.yin 2021/8/17 15:44
 */
@RestController
@RequestMapping("/user")
@ApiIgnore
public class UserController extends BaseController {

    private static Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @Title: login
     * @author: dy.yin 2021/8/17 16:04
     * @param: [accountName, passWord, request]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping("/login")
    public Object login(@RequestBody User user) {
        String accountName = user.getAccountName();
        String passWord = user.getPassWord();
        if (StringUtils.isEmpty(accountName) || StringUtils.isEmpty(passWord)) {
            return "请输入用户名和密码！";
        }

        user = userService.loadUserByUsername(accountName,passWord);
        user.setPassWord(null);
        String token = JwtTokenUtil.creatToken(user,JwtTokenUtil.MANAGE_SECRET);
        Map<String,Object> data = new HashMap<>();
        data.put("token",token);
        data.put("user",user);
        data.put("project","/manage");

        return retResultVo(data);
    }
}
